package com.demo;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Person implements ApplicationListener {
    public int pId;
    public String pName;
    public int pAge;
    public List pHobby;
    // 强类型集合  Setter注入
    public Map<Integer,String> pPhoto;


    public int getpAge() {
        return pAge;
    }

    public void setpAge(int pAge) {
        this.pAge = pAge;
    }

    public Person() {
        System.out.println("无参的constructor  构造器依赖注入");
    }

    public int getpId() {
        return pId;
    }

    public void setpId(int pId) {
        System.out.println("--->Setter 依赖注入 pId= "+pId);
        this.pId = pId;
    }

    public String getpName() {
        return pName;
    }

    public void setpName(String pName) {
        this.pName = pName;
    }

    public void setpHobby(List pHobby) {
        this.pHobby = pHobby;
    }

    public void setpPhoto(Map<Integer, String> pPhoto) {
        this.pPhoto = pPhoto;
    }

    public Person(int pId, String pName, List pHobby) {
        System.out.println("有参的constructor  构造器依赖注入");
        this.pId = pId;
        this.pName = pName;
        this.pHobby = pHobby;
    }

    @Override
    public String toString() {
        return "Person{" +
                "pId=" + pId +
                ", pName='" + pName + '\'' +
                ", pAge=" + pAge +
                ", pHobby=" + pHobby +
                ", pPhoto=" + pPhoto +
                '}';
    }

    /**
     * 初始化回调
     */
    public void init() {
        System.out.println("init pId=" + (this.pId+1));
    }

    /**
     * 销毁回调
     */
    public void cleanup() {
        this.pId=0;
        this.pName="NULL";
        System.out.println(this.toString());
    }

    /**
     * 得到通知,通知方法
     * @param applicationEvent
     */
    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        if (applicationEvent instanceof PersonListEvent) {

            Person person=((PersonListEvent) applicationEvent).person;
            //System.out.println(pId);
            if (person.pId==pId){
                System.out.println(person.pName+"( id为"+ person.pId+") 被通知 "+((PersonListEvent) applicationEvent).message );
            }

            // notify appropriate person...
        }
    }
}
